package com.ttc.mBackup.BackupSMS;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class ProgressDialogPerform {
	
	private static final String CHECK = "check";
	private ProgressDialog mProgressDialog;
	private SMSStatistics sms;
	private int backupMode;
	private Context context;
	private String filename;
	private ArrayList<String> phoneContact;
	private int locationCode;
	private boolean flag = false;
	
	/*
	 * Constructor
	 */
	public ProgressDialogPerform(Context context) {
		super();
		this.context = context;
		sms = new SMSStatistics(context);
	}
	/*
	 * Set Backup Mode
	 * @param: code is the code of backup mode
	 */
	public void setBackupMode(int code) {
		this.backupMode = code;
		
	}
	/*
	 * set the backup filename
	 * @param: filename is String of the backup filename
	 */
	public void setFilename(String filename) {
		this.filename = filename;
	}

	public void setPhoneContact(ArrayList<String> phoneContact) {
		this.phoneContact = phoneContact;
	}

	public void setLocationCode(int locationCode) {
		this.locationCode = locationCode;
	}

	protected void onCreate() {
		new Perform().execute("");
	}
	
	protected ProgressDialog getDialog(){
		return mProgressDialog;
	}
	
	protected ProgressDialog onCreateDialog(int id) {
		
		mProgressDialog = new ProgressDialog(context);
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setCancelable(false);
		switch (id) {
			case DialogCode.CODE_PROGRESS_DELETE:
	            mProgressDialog.setMessage("Deleting File...");
	            return mProgressDialog;
	            
			case DialogCode.CODE_PROGRESS_RESTORE_OVERRIDE:
			case DialogCode.CODE_PROGRESS_RESTORE_INCREMENTAL:
	            mProgressDialog.setMessage("Restore Message..");
	            return mProgressDialog;
	            
			case DialogCode.CODE_PROGRESS_BACKUP_NEW:
			case DialogCode.CODE_PROGRESS_BACKUP_INCREMENTAL:
	            mProgressDialog.setMessage("Backup Message..");
	            return mProgressDialog;
		default:
			return null;
		}
	}
	
class Perform extends AsyncTask<String, String, String> {
        
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            ProgressDialog dialog = onCreateDialog(backupMode);
            dialog.show();
            Log.v(CHECK, "Show Dialog Successfully");
        }
        
        @Override
        protected String doInBackground(String... params) {
        	
            try {
            	switch (backupMode) {
            	//case of backup mode
					case DialogCode.CODE_PROGRESS_BACKUP_NEW:
					case DialogCode.CODE_PROGRESS_BACKUP_INCREMENTAL:
						BackupSMS backupsms = new BackupSMS(sms, 
														phoneContact, 
														filename, locationCode, backupMode);
            			int i = 0;
            			mProgressDialog.setMax(1);
            			flag = backupsms.createBackupFile();
            			Log.v("check", "" + flag);
            			while (i != 1) {
        					publishProgress(i++ + "");
            			}
            			break;
            	//case of restore mode
				case DialogCode.CODE_PROGRESS_RESTORE_OVERRIDE:
				case DialogCode.CODE_PROGRESS_RESTORE_INCREMENTAL:
						RestoreSMS restoresms = new RestoreSMS(sms, 
														phoneContact, 
														filename, locationCode, backupMode);
            			
            			int j = 0;
            			mProgressDialog.setMax(1);
            			flag = restoresms.RestoreMessage();
            			Log.v("check", "" + flag);
            			while (j != 1) {
            				publishProgress(j++ + "");
            			}
            			break;
            		//case of deltete file
					case DialogCode.CODE_PROGRESS_DELETE:
						int x = 0;
						flag = new FileBrowser(filename).DeleleFile();
						while (x != 1) {
							publishProgress(x++ + "");
						}
						break;
					default:
						break;
            	}
            		
            } catch (Exception e) {
            	Log.v(CHECK, "Thread Problem " + e.getMessage());
            }
            return null;
        }
        
        protected void onProgressUpdate(String... progress) {
             mProgressDialog.setProgress(Integer.parseInt(progress[0]));
        }
        
        

        @Override
        protected void onPostExecute(String unused) {
        	mProgressDialog.dismiss();
        	if(flag){
        		if(backupMode == DialogCode.CODE_PROGRESS_BACKUP_NEW 
            			|| backupMode == DialogCode.CODE_PROGRESS_BACKUP_INCREMENTAL){
            		new CustomDialog(context).createDialogResult("Backup Message Successfully");
            	}else if(backupMode == DialogCode.CODE_PROGRESS_RESTORE_INCREMENTAL 
            			|| backupMode == DialogCode.CODE_PROGRESS_RESTORE_OVERRIDE){
            		new CustomDialog(context).createDialogResult("Restore Message Successfully");
            	}else if(backupMode == DialogCode.CODE_PROGRESS_DELETE){
            		new CustomDialog(context).createDialogResult("Delete File Successfully");
            	}
        	}else{
        		if(backupMode == DialogCode.CODE_PROGRESS_BACKUP_NEW 
            			|| backupMode == DialogCode.CODE_PROGRESS_BACKUP_INCREMENTAL){
        			new CustomDialog(context).createDialogResult("Unable Backup");
            	}else if(backupMode == DialogCode.CODE_PROGRESS_RESTORE_INCREMENTAL 
            			|| backupMode == DialogCode.CODE_PROGRESS_RESTORE_OVERRIDE){
            		new CustomDialog(context).createDialogResult("Unable Restore");
            	}else if(backupMode == DialogCode.CODE_PROGRESS_DELETE){
            		new CustomDialog(context).createDialogResult("Unable Delete File");
            	}
        	} 	
        }
    }
}
